using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using CatalogSystemProxy;

namespace CatalogManager.CatalogForms
{
	/// <summary>
	/// Summary description for CatalogEditor.
	/// </summary>
	public class CatalogEditor : System.Windows.Forms.Form
	{
		private DataRow catalogProperties = null;
		private string defaultLanguage= null;
		private string reportingLangague=null;
		private DataRow drcatlgEdtitor=null;
		private DataSet catLanguages = null;
		public DataSet Languages = null;
		public DataSet DependentCatalogs = null;
		public DataSet Currencies = null;
		public bool newCatalog=false;

		private System.Windows.Forms.Label lblCatalogName;
		private System.Windows.Forms.Label lblStartDate;
		private System.Windows.Forms.Label lblEndDate;
		private System.Windows.Forms.Label lblCurrency;
		private System.Windows.Forms.Label lblUnit;
		private System.Windows.Forms.Label lblProductID;
		private System.Windows.Forms.Label lblVariantID;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnAddLanguage;
		private System.Windows.Forms.Label lblDefaultLang;
		private System.Windows.Forms.Label lblReportingLang;
		private System.Windows.Forms.ComboBox cboDefaultLang;
		private System.Windows.Forms.ComboBox cboReportingLang;
		private System.Windows.Forms.TextBox txtCataogName;
		private System.Windows.Forms.TextBox txtProductID;
		private System.Windows.Forms.TextBox txtVariantID;
		private System.Windows.Forms.Button btnBack;
		private System.Windows.Forms.Button btnSaveNBack;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnRemoveLang;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.DateTimePicker StartdateTimePicker;
		private System.Windows.Forms.ListBox ListBoxLanguages;
		private System.Windows.Forms.ListBox DependentCataloglistBox;
		private System.Windows.Forms.Label lblDependentCatalogs;
		private System.Windows.Forms.TextBox txtUnit;
		private System.Windows.Forms.ComboBox comboBoxCurrencies;
		private System.Windows.Forms.ListView listViewDisplayName;
		private System.Windows.Forms.DataGrid dataGridDisplayName;
		private System.Windows.Forms.DateTimePicker EnddateTimePicker;
		private System.Windows.Forms.Button btnAccept;
		private System.Windows.Forms.Button btnCancel;
		private int idxDisplayName=-1;

		private CatalogSystem CSService = null;

		public CatalogEditor()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.lblCatalogName = new System.Windows.Forms.Label();
			this.lblStartDate = new System.Windows.Forms.Label();
			this.lblEndDate = new System.Windows.Forms.Label();
			this.lblCurrency = new System.Windows.Forms.Label();
			this.lblUnit = new System.Windows.Forms.Label();
			this.lblProductID = new System.Windows.Forms.Label();
			this.lblVariantID = new System.Windows.Forms.Label();
			this.txtCataogName = new System.Windows.Forms.TextBox();
			this.txtUnit = new System.Windows.Forms.TextBox();
			this.txtProductID = new System.Windows.Forms.TextBox();
			this.txtVariantID = new System.Windows.Forms.TextBox();
			this.ListBoxLanguages = new System.Windows.Forms.ListBox();
			this.label1 = new System.Windows.Forms.Label();
			this.btnAddLanguage = new System.Windows.Forms.Button();
			this.btnRemoveLang = new System.Windows.Forms.Button();
			this.lblDefaultLang = new System.Windows.Forms.Label();
			this.lblReportingLang = new System.Windows.Forms.Label();
			this.cboDefaultLang = new System.Windows.Forms.ComboBox();
			this.cboReportingLang = new System.Windows.Forms.ComboBox();
			this.btnBack = new System.Windows.Forms.Button();
			this.btnSaveNBack = new System.Windows.Forms.Button();
			this.btnSave = new System.Windows.Forms.Button();
			this.StartdateTimePicker = new System.Windows.Forms.DateTimePicker();
			this.EnddateTimePicker = new System.Windows.Forms.DateTimePicker();
			this.DependentCataloglistBox = new System.Windows.Forms.ListBox();
			this.lblDependentCatalogs = new System.Windows.Forms.Label();
			this.comboBoxCurrencies = new System.Windows.Forms.ComboBox();
			this.listViewDisplayName = new System.Windows.Forms.ListView();
			this.dataGridDisplayName = new System.Windows.Forms.DataGrid();
			this.btnAccept = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.dataGridDisplayName)).BeginInit();
			this.SuspendLayout();
			// 
			// lblCatalogName
			// 
			this.lblCatalogName.Location = new System.Drawing.Point(16, 16);
			this.lblCatalogName.Name = "lblCatalogName";
			this.lblCatalogName.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.lblCatalogName.Size = new System.Drawing.Size(128, 16);
			this.lblCatalogName.TabIndex = 0;
			this.lblCatalogName.Text = "Catalog Name";
			// 
			// lblStartDate
			// 
			this.lblStartDate.Location = new System.Drawing.Point(16, 40);
			this.lblStartDate.Name = "lblStartDate";
			this.lblStartDate.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.lblStartDate.Size = new System.Drawing.Size(128, 16);
			this.lblStartDate.TabIndex = 1;
			this.lblStartDate.Text = "Start Date";
			// 
			// lblEndDate
			// 
			this.lblEndDate.Location = new System.Drawing.Point(16, 64);
			this.lblEndDate.Name = "lblEndDate";
			this.lblEndDate.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.lblEndDate.Size = new System.Drawing.Size(128, 16);
			this.lblEndDate.TabIndex = 2;
			this.lblEndDate.Text = "End Date";
			// 
			// lblCurrency
			// 
			this.lblCurrency.Location = new System.Drawing.Point(16, 80);
			this.lblCurrency.Name = "lblCurrency";
			this.lblCurrency.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.lblCurrency.Size = new System.Drawing.Size(128, 16);
			this.lblCurrency.TabIndex = 3;
			this.lblCurrency.Text = "Currency";
			// 
			// lblUnit
			// 
			this.lblUnit.Location = new System.Drawing.Point(16, 104);
			this.lblUnit.Name = "lblUnit";
			this.lblUnit.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.lblUnit.Size = new System.Drawing.Size(128, 23);
			this.lblUnit.TabIndex = 4;
			this.lblUnit.Text = "Unit of weight measure";
			// 
			// lblProductID
			// 
			this.lblProductID.Location = new System.Drawing.Point(16, 128);
			this.lblProductID.Name = "lblProductID";
			this.lblProductID.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.lblProductID.Size = new System.Drawing.Size(128, 16);
			this.lblProductID.TabIndex = 5;
			this.lblProductID.Text = "Product unique ID";
			// 
			// lblVariantID
			// 
			this.lblVariantID.Location = new System.Drawing.Point(8, 152);
			this.lblVariantID.Name = "lblVariantID";
			this.lblVariantID.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.lblVariantID.Size = new System.Drawing.Size(136, 23);
			this.lblVariantID.TabIndex = 6;
			this.lblVariantID.Text = "Product variant unique ID";
			// 
			// txtCataogName
			// 
			this.txtCataogName.Enabled = false;
			this.txtCataogName.Location = new System.Drawing.Point(152, 8);
			this.txtCataogName.Name = "txtCataogName";
			this.txtCataogName.Size = new System.Drawing.Size(248, 20);
			this.txtCataogName.TabIndex = 7;
			this.txtCataogName.Text = "";
			// 
			// txtUnit
			// 
			this.txtUnit.Enabled = false;
			this.txtUnit.Location = new System.Drawing.Point(152, 104);
			this.txtUnit.Name = "txtUnit";
			this.txtUnit.Size = new System.Drawing.Size(248, 20);
			this.txtUnit.TabIndex = 10;
			this.txtUnit.Text = "";
			// 
			// txtProductID
			// 
			this.txtProductID.Enabled = false;
			this.txtProductID.Location = new System.Drawing.Point(152, 128);
			this.txtProductID.Name = "txtProductID";
			this.txtProductID.Size = new System.Drawing.Size(248, 20);
			this.txtProductID.TabIndex = 12;
			this.txtProductID.Text = "";
			// 
			// txtVariantID
			// 
			this.txtVariantID.Enabled = false;
			this.txtVariantID.Location = new System.Drawing.Point(152, 152);
			this.txtVariantID.Name = "txtVariantID";
			this.txtVariantID.Size = new System.Drawing.Size(248, 20);
			this.txtVariantID.TabIndex = 13;
			this.txtVariantID.Text = "";
			// 
			// ListBoxLanguages
			// 
			this.ListBoxLanguages.Location = new System.Drawing.Point(8, 208);
			this.ListBoxLanguages.Name = "ListBoxLanguages";
			this.ListBoxLanguages.Size = new System.Drawing.Size(392, 108);
			this.ListBoxLanguages.TabIndex = 14;
			this.ListBoxLanguages.SelectedIndexChanged += new System.EventHandler(this.ListBoxLanguages_SelectedIndexChanged);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 184);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(136, 16);
			this.label1.TabIndex = 15;
			this.label1.Text = "Languages";
			// 
			// btnAddLanguage
			// 
			this.btnAddLanguage.Location = new System.Drawing.Point(152, 176);
			this.btnAddLanguage.Name = "btnAddLanguage";
			this.btnAddLanguage.Size = new System.Drawing.Size(104, 23);
			this.btnAddLanguage.TabIndex = 16;
			this.btnAddLanguage.Text = "Add Language";
			this.btnAddLanguage.Click += new System.EventHandler(this.btnAddLanguage_Click);
			// 
			// btnRemoveLang
			// 
			this.btnRemoveLang.Enabled = false;
			this.btnRemoveLang.Location = new System.Drawing.Point(264, 176);
			this.btnRemoveLang.Name = "btnRemoveLang";
			this.btnRemoveLang.Size = new System.Drawing.Size(104, 23);
			this.btnRemoveLang.TabIndex = 17;
			this.btnRemoveLang.Text = "Remove Lanuage";
			this.btnRemoveLang.Click += new System.EventHandler(this.btnRemoveLang_Click);
			// 
			// lblDefaultLang
			// 
			this.lblDefaultLang.Location = new System.Drawing.Point(8, 320);
			this.lblDefaultLang.Name = "lblDefaultLang";
			this.lblDefaultLang.Size = new System.Drawing.Size(136, 16);
			this.lblDefaultLang.TabIndex = 18;
			this.lblDefaultLang.Text = "Default Lnaguage";
			// 
			// lblReportingLang
			// 
			this.lblReportingLang.Location = new System.Drawing.Point(8, 344);
			this.lblReportingLang.Name = "lblReportingLang";
			this.lblReportingLang.Size = new System.Drawing.Size(136, 16);
			this.lblReportingLang.TabIndex = 19;
			this.lblReportingLang.Text = "Reporting Language";
			// 
			// cboDefaultLang
			// 
			this.cboDefaultLang.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboDefaultLang.Location = new System.Drawing.Point(152, 320);
			this.cboDefaultLang.Name = "cboDefaultLang";
			this.cboDefaultLang.Size = new System.Drawing.Size(248, 21);
			this.cboDefaultLang.TabIndex = 20;
			// 
			// cboReportingLang
			// 
			this.cboReportingLang.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboReportingLang.Location = new System.Drawing.Point(152, 344);
			this.cboReportingLang.Name = "cboReportingLang";
			this.cboReportingLang.Size = new System.Drawing.Size(248, 21);
			this.cboReportingLang.TabIndex = 21;
			// 
			// btnBack
			// 
			this.btnBack.Location = new System.Drawing.Point(40, 376);
			this.btnBack.Name = "btnBack";
			this.btnBack.Size = new System.Drawing.Size(104, 23);
			this.btnBack.TabIndex = 22;
			this.btnBack.Text = "Back";
			this.btnBack.Click += new System.EventHandler(this.btnBack_Click);
			// 
			// btnSaveNBack
			// 
			this.btnSaveNBack.Location = new System.Drawing.Point(152, 376);
			this.btnSaveNBack.Name = "btnSaveNBack";
			this.btnSaveNBack.Size = new System.Drawing.Size(104, 23);
			this.btnSaveNBack.TabIndex = 23;
			this.btnSaveNBack.Text = "Save and Back";
			this.btnSaveNBack.Click += new System.EventHandler(this.btnSaveNBack_Click);
			// 
			// btnSave
			// 
			this.btnSave.Location = new System.Drawing.Point(264, 376);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(104, 23);
			this.btnSave.TabIndex = 24;
			this.btnSave.Text = "Save";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// StartdateTimePicker
			// 
			this.StartdateTimePicker.Location = new System.Drawing.Point(152, 32);
			this.StartdateTimePicker.Name = "StartdateTimePicker";
			this.StartdateTimePicker.Size = new System.Drawing.Size(248, 20);
			this.StartdateTimePicker.TabIndex = 25;
			// 
			// EnddateTimePicker
			// 
			this.EnddateTimePicker.Location = new System.Drawing.Point(152, 56);
			this.EnddateTimePicker.Name = "EnddateTimePicker";
			this.EnddateTimePicker.Size = new System.Drawing.Size(248, 20);
			this.EnddateTimePicker.TabIndex = 26;
			// 
			// DependentCataloglistBox
			// 
			this.DependentCataloglistBox.Location = new System.Drawing.Point(8, 592);
			this.DependentCataloglistBox.Name = "DependentCataloglistBox";
			this.DependentCataloglistBox.Size = new System.Drawing.Size(392, 56);
			this.DependentCataloglistBox.TabIndex = 27;
			// 
			// lblDependentCatalogs
			// 
			this.lblDependentCatalogs.Location = new System.Drawing.Point(8, 568);
			this.lblDependentCatalogs.Name = "lblDependentCatalogs";
			this.lblDependentCatalogs.Size = new System.Drawing.Size(152, 16);
			this.lblDependentCatalogs.TabIndex = 28;
			this.lblDependentCatalogs.Text = "Dependent Catalogs:";
			// 
			// comboBoxCurrencies
			// 
			this.comboBoxCurrencies.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBoxCurrencies.Location = new System.Drawing.Point(152, 80);
			this.comboBoxCurrencies.Name = "comboBoxCurrencies";
			this.comboBoxCurrencies.Size = new System.Drawing.Size(248, 21);
			this.comboBoxCurrencies.TabIndex = 29;
			// 
			// listViewDisplayName
			// 
			this.listViewDisplayName.FullRowSelect = true;
			this.listViewDisplayName.Location = new System.Drawing.Point(8, 408);
			this.listViewDisplayName.MultiSelect = false;
			this.listViewDisplayName.Name = "listViewDisplayName";
			this.listViewDisplayName.Size = new System.Drawing.Size(392, 72);
			this.listViewDisplayName.TabIndex = 30;
			this.listViewDisplayName.Click += new System.EventHandler(this.listViewDisplayName_Click);
			// 
			// dataGridDisplayName
			// 
			this.dataGridDisplayName.CaptionVisible = false;
			this.dataGridDisplayName.DataMember = "";
			this.dataGridDisplayName.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGridDisplayName.Location = new System.Drawing.Point(8, 488);
			this.dataGridDisplayName.Name = "dataGridDisplayName";
			this.dataGridDisplayName.Size = new System.Drawing.Size(392, 40);
			this.dataGridDisplayName.TabIndex = 31;
			// 
			// btnAccept
			// 
			this.btnAccept.Enabled = false;
			this.btnAccept.Location = new System.Drawing.Point(88, 536);
			this.btnAccept.Name = "btnAccept";
			this.btnAccept.Size = new System.Drawing.Size(104, 23);
			this.btnAccept.TabIndex = 33;
			this.btnAccept.Text = "Accept";
			this.btnAccept.Click += new System.EventHandler(this.btnAccept_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(200, 536);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(104, 23);
			this.btnCancel.TabIndex = 34;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// CatalogEditor
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(406, 652);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnCancel,
																		  this.btnAccept,
																		  this.dataGridDisplayName,
																		  this.listViewDisplayName,
																		  this.comboBoxCurrencies,
																		  this.lblDependentCatalogs,
																		  this.DependentCataloglistBox,
																		  this.EnddateTimePicker,
																		  this.StartdateTimePicker,
																		  this.btnSave,
																		  this.btnSaveNBack,
																		  this.btnBack,
																		  this.cboReportingLang,
																		  this.cboDefaultLang,
																		  this.lblReportingLang,
																		  this.lblDefaultLang,
																		  this.btnRemoveLang,
																		  this.btnAddLanguage,
																		  this.label1,
																		  this.ListBoxLanguages,
																		  this.txtVariantID,
																		  this.txtProductID,
																		  this.txtUnit,
																		  this.txtCataogName,
																		  this.lblVariantID,
																		  this.lblProductID,
																		  this.lblUnit,
																		  this.lblCurrency,
																		  this.lblEndDate,
																		  this.lblStartDate,
																		  this.lblCatalogName});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "CatalogEditor";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Catalog Editor";
			this.Load += new System.EventHandler(this.CatalogEditor_Load);
			((System.ComponentModel.ISupportInitialize)(this.dataGridDisplayName)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void CatalogEditor_Load(object sender, System.EventArgs e)
		{
			if(newCatalog)
			{
				//
			}
			else
			{
				// read the xml file and get the file path
				XmlDocument doc = new XmlDocument();
				doc.Load("SiteConfig.xml");
				CSService = new CatalogSystem(doc.FirstChild.InnerText);

				// load all of the properties
				txtCataogName.Text = drcatlgEdtitor["CatalogName"].ToString();
				StartdateTimePicker.Text = drcatlgEdtitor["StartDate"].ToString();
				EnddateTimePicker.Text = drcatlgEdtitor["EndDate"].ToString();
				txtUnit.Text = drcatlgEdtitor["WeightMeasure"].ToString();
				txtProductID.Text = drcatlgEdtitor["ProductID"].ToString();
				txtVariantID.Text = drcatlgEdtitor["VariantID"].ToString();

				// now get the default language and reporting language
				string DefaultLanguage = drcatlgEdtitor["DefaultLanguage"].ToString();
				string ReportingLanguage = drcatlgEdtitor["ReportingLanguage"].ToString();

				// add a column we need to keep track of what was added and what was removed
				catLanguages.Tables[0].Columns.Add("Removed", typeof(string));
				catLanguages.Tables[0].Columns.Add("display_name", typeof(string));

				// now fill the catalog languages
				foreach(DataRow dr in catLanguages.Tables[0].Rows)
				{
					string lang = dr["Language"].ToString();
					DataRow[] row = Languages.Tables[0].Select("culture_name='"+lang+"'");
					string rowValue = row[0]["display_name"].ToString();
					// now the language display name
					dr["display_name"] = rowValue;
					int defIdx = cboDefaultLang.Items.Add(rowValue);
					int repIdx = cboReportingLang.Items.Add(rowValue);
					ListBoxLanguages.Items.Add(rowValue);

					if(lang == DefaultLanguage)
					{
						cboDefaultLang.SelectedIndex = defIdx;
					}
					if(lang == ReportingLanguage)
					{
						cboReportingLang.SelectedIndex = repIdx;
					}
					// mark removed values
					dr["Removed"]="0";
				}

				int idxCurrency=-1;

				// now get Unit of measure
				foreach(DataRow drCurrency in Currencies.Tables[0].Rows)
				{
					idxCurrency = comboBoxCurrencies.Items.Add(drCurrency["iso4217"].ToString());
					if(drcatlgEdtitor["Currency"].ToString()==drCurrency["iso4217"].ToString())
					{
						comboBoxCurrencies.SelectedIndex = idxCurrency;
					}
				}
				// get the displaynems
				listViewDisplayName.Items.Clear();
				listViewDisplayName.Columns.Clear();
				listViewDisplayName.Columns.Add("Language", 125, HorizontalAlignment.Left);
				listViewDisplayName.Columns.Add("Display Name", 125, HorizontalAlignment.Left);
				listViewDisplayName.AllowColumnReorder = true;
				listViewDisplayName.View = View.Details;
				foreach(DataColumn dc in drcatlgEdtitor.Table.Columns)
				{
					string columName = dc.ColumnName.Replace("DisplayName_","");
					DataRow[] drculture = Languages.Tables[0].Select("culture_name='"+columName+"'");
					if(drculture.Length>0)
					{
						ListViewItem lv = new ListViewItem(drculture[0]["display_name"].ToString());
						lv.SubItems.Add(drcatlgEdtitor[dc.ColumnName].ToString());
						listViewDisplayName.Items.Add(lv);
					}
				}
				// now get dependent catalog
				try
				{
					if(DependentCatalogs!=null)
					{
						foreach(DataRow drDependentCtlg in DependentCatalogs.Tables[0].Rows)
						{
							DependentCataloglistBox.Items.Add(drDependentCtlg["CatalogName"].ToString());
						}
					}
				}
				catch{}

				// do some magic here disable the Display name so we can't change the properties
				DataGridTableStyle dgStyle = new DataGridTableStyle();
				dgStyle.AllowSorting = false;
				dgStyle.MappingName = "Table1";
				DataGridTextBoxColumn DisplayName = new DataGridTextBoxColumn();
				DisplayName.MappingName = "Language";
				DisplayName.HeaderText = "Language";
				DisplayName.Width = 150;
				DisplayName.ReadOnly = true;
				dgStyle.GridColumnStyles.Add(DisplayName);
				// Add a second column style.
				DataGridColumnStyle Value = new DataGridTextBoxColumn();
				Value.MappingName = "Display Name";
				Value.HeaderText = "Display Name";
				Value.Width = 200;
				Value.ReadOnly = false;
				dgStyle.GridColumnStyles.Add(Value);
				dataGridDisplayName.TableStyles.Add(dgStyle);
			}
		}

		private void btnAddLanguage_Click(object sender, System.EventArgs e)
		{
			// show the language selector
			LanguageSelector ls = new LanguageSelector();
			ls.Languages = Languages;
			ls.ShowDialog(this);
			// get all selected languages
			IEnumerator ie = ls.LanguagecheckedListBox.CheckedItems.GetEnumerator();
			while(ie.MoveNext())
			{
				// find out if we already have this language
				DataRow[] dr = catLanguages.Tables[0].Select("display_name='"+ie.Current.ToString()+"'");
				// if we already have the language then don't add it to the list if we already had it
				if(dr.Length>0)
				{
					// if it's a new record then add it
					if(dr[0]["Removed"].ToString()=="1")
					{
						dr[0]["Removed"]="0";
						// don't add it to the dataset just add it to the columns
						AddLanguage(ie.Current.ToString());
					}
				}
				else
				{
					// add the record it's new
					DataRow[] drLanguages = Languages.Tables[0].Select("display_name='"+ie.Current.ToString()+"'");
					DataRow drLang = catLanguages.Tables[0].NewRow();
					drLang["Removed"] = "2";
					drLang["Language"] = drLanguages[0]["culture_name"].ToString();
					drLang["display_name"] = ie.Current.ToString();
					catLanguages.Tables[0].Rows.Add(drLang);
					AddLanguage(ie.Current.ToString());
				}
			}
		}

		private void AddLanguage(string language)
		{
			ListBoxLanguages.Items.Add(language);
			cboDefaultLang.Items.Add(language);
			cboReportingLang.Items.Add(language);
		}

		private void btnRemoveLang_Click(object sender, System.EventArgs e)
		{
			// we need to make sure that we keep at least one
			if(ListBoxLanguages.Items.Count>1)
			{
				string language = ListBoxLanguages.SelectedItem.ToString();
				// mark the dataset
				DataRow[] dr = catLanguages.Tables[0].Select("display_name='"+language+"'");
				if(dr[0]["Removed"].ToString()=="0")
				{
					// mark it as '1' for existing record to be removed
					dr[0]["Removed"] = "1";
				}
				else if(dr[0]["Removed"].ToString()=="2")
				{
					// remove the row that was added as new but was not saved
					catLanguages.Tables[0].Rows.Remove(dr[0]);
				}
				// remove the language from the listbox and dropdowns
				cboDefaultLang.Items.Remove(language);
				cboReportingLang.Items.Remove(language);
				ListBoxLanguages.Items.Remove(language);
			}
		}

		private void btnSaveNBack_Click(object sender, System.EventArgs e)
		{
			// save properties and exit
			Save();
			this.Close();			
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			// call the save
			Save();
		}

		private void btnBack_Click(object sender, System.EventArgs e)
		{
			// exit
			this.Close();
		}

		private void Save()
		{
			// create one dataset and send that for save
			DataSet ds = new DataSet("CatalogEditor");
			// CatalogProperties
			DataTable dtProp = ds.Tables.Add("CatalogProperties");
			DataTable dtLang = ds.Tables.Add();
			DataTable dtDefualtLang = ds.Tables.Add("DefaultLanguages");
			// create columns for Catalog Properties
			dtProp.Columns.Add("StartDate", typeof(string));
			dtProp.Columns.Add("EndDate", typeof(string));
			dtProp.Columns.Add("Currency", typeof(string));
			// create columns for displaynames
			foreach(DataColumn col in drcatlgEdtitor.Table.Columns)
			{
				if(col.ColumnName.StartsWith("DisplayName_"))
				{
					dtProp.Columns.Add(col.ColumnName, typeof(string));
				}
			}
			// now add the display names

			// create columns for Catalaog Default Languages
			dtDefualtLang.Columns.Add("DefaultLanguage", typeof(string));
			dtDefualtLang.Columns.Add("DefaultLanguageRep", typeof(string));
			// add rows for Catalog Properties
			DataRow drProp = dtProp.NewRow();
			drProp["StartDate"] = StartdateTimePicker.Text;
			drProp["EndDate"] = EnddateTimePicker.Text;
			drProp["Currency"] = comboBoxCurrencies.Text;
			// now add our displayname values
			foreach(DataColumn colLang in drProp.Table.Columns)
			{
				// now get the displayanme columns
				if(colLang.ColumnName.StartsWith("DisplayName_"))
				{
					drProp[colLang.ColumnName]=drcatlgEdtitor[colLang.ColumnName].ToString();
				}				
			}
			dtProp.Rows.Add(drProp);
			// get the language table
			dtLang = catLanguages.Tables[0].Copy();
			ds.Tables.Add(dtLang);
			// now add our default languages
			DataRow drDefualtLang = dtDefualtLang.NewRow();
			// find the culture name
			DataRow[] drculture = Languages.Tables[0].Select("display_name='"+cboDefaultLang.Text+"'");
			drDefualtLang["DefaultLanguage"] = drculture[0]["culture_name"].ToString();
			drculture = Languages.Tables[0].Select("display_name='"+cboReportingLang.Text+"'");
			drDefualtLang["DefaultLanguageRep"] = drculture[0]["culture_name"].ToString();
			dtDefualtLang.Rows.Add(drDefualtLang);
			// for testing purposes write to a file
			//ds.WriteXml(@"c:\test.xml");
			// now call the webservice and save
			CSService.SaveCatalogProperties(ds, txtCataogName.Text);
		}

		private void ListBoxLanguages_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// enable the remove button
			if(ListBoxLanguages.SelectedIndex == -1)
			{
				btnRemoveLang.Enabled = false;
			}
			else
			{
				btnRemoveLang.Enabled = true;
			}
		}

		private void listViewDisplayName_Click(object sender, System.EventArgs e)
		{
			btnAccept.Enabled = true;
			dataGridDisplayName.DataSource = null;
			// clear our the grid then get the variant
			idxDisplayName = listViewDisplayName.SelectedItems[0].Index;
			ListViewItem lv = listViewDisplayName.SelectedItems[0];
			DataSet ds = new DataSet();
			ds.Tables.Add(new DataTable());
			for(int i=0;listViewDisplayName.Columns.Count>i;i++)
			{
				ds.Tables[0].Columns.Add(listViewDisplayName.Columns[i].Text, typeof(string));	
			}

			DataRow dr = ds.Tables[0].NewRow();

			for(int i=0;listViewDisplayName.Columns.Count>i;i++)
			{
				dr[listViewDisplayName.Columns[i].Text] = lv.SubItems[i].Text;
			}
			ds.Tables[0].Rows.Add(dr);
			dataGridDisplayName.DataSource = ds.Tables[0];
			CurrencyManager cm = (CurrencyManager)this.BindingContext[dataGridDisplayName.DataSource, dataGridDisplayName.DataMember];
			((DataView)cm.List).AllowNew = false;
		}

		private void btnAccept_Click(object sender, System.EventArgs e)
		{
			// accept user changes and change the listview
			ListViewItem li = listViewDisplayName.Items[idxDisplayName];
			li.SubItems[1].Text = ((DataTable)dataGridDisplayName.DataSource).Rows[0][1].ToString();
			// now update the dataset
			DataRow[] drculture = Languages.Tables[0].Select("display_name='"+li.SubItems[0].Text+"'");
			drcatlgEdtitor["DisplayName_"+drculture[0]["culture_name"].ToString()]=li.SubItems[1].Text;
			dataGridDisplayName.DataSource = null;
			btnAccept.Enabled = false;
			idxDisplayName = -1;
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			// clear the data list
			btnAccept.Enabled = false;
			idxDisplayName = -1;
			dataGridDisplayName.DataSource = null;
		}

		public DataRow CatalogPropertiess
		{
			set
			{
				catalogProperties = value;
			}
		}

		public string ReportingLanguage
		{
			set
			{
				defaultLanguage = value;
			}
		}

		public string DefaultLanguage
		{
			set
			{
				reportingLangague = value;
			}
		}

		public DataSet CatLanguages
		{
			get
			{
				return catLanguages;
			}
			set
			{
				catLanguages=value;
			}
		}

		public DataRow drCatalogEdtitor
		{
			get
			{
				return drcatlgEdtitor;
			}
			set
			{
				drcatlgEdtitor=value;
			}
		}
	}
}
